package com.meng.learn.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Bean
    public Docket docket(Environment environment){
//        Profiles profiles = Profiles.of("dev", "fat"); // 设置在那个环境下显示swagger,这里设置环境为dev时显示
        Profiles profiles = Profiles.of("fat"); // 设置在那个环境下显示swagger,这里设置环境为dev时显示
        boolean b = environment.acceptsProfiles(profiles); // 判断是不是现在的环境是不是我们想要的环境
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(b) // 配置swagger是否开启，如果为false则关闭swagger，默认为true
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.meng.learn.swagger.controller"))
                .paths(PathSelectors.any())
                .build();

    }

    private ApiInfo apiInfo() {

        return new ApiInfoBuilder()
                .title("swagger demo")
                .description("测试 swagger")
                .contact(new Contact("pp","com.mengpp.com","1211694581@qq.com"))
                .version("1.0")
                .build();
    }
}
